<?php

class ClientController extends Controller
{
	public $layout='/';
	public function actionIndex()
	{
		$content = "";
		$this->render('index', array('content'=>$content));
	}

	public function actionGetEvents()
	{
		$model = Events::model()->FindAll(array('order'=>'name'));
		$content = "<response>\n";
		
		foreach ($model as $m) {
			$content .= "    <event>\n";
			$content .= "      <id>{$m->event_id}</id>\n";
			$content .= "      <name>{$m->name}</name>\n";
			$content .= "    </event>\n";
		}
		
		$content .= "</response>\n";
		$this->render('index', array('content'=>$content));
	}
	
	public function actionCheckIn($e,$b)
	{
		$model = AttendingEvent::model()->Find('event_id=:eid AND barcode_value=:bv', array(':eid'=>$e, ':bv'=>$b));
		$content = "<response>\n";
		if (is_null($model)) {
			//Invalid Barcode
			$content .= "    <status>Invalid</status>\n";
		} else if ($model->checked_in != 0) {
			//Already Checked In
			
			$guest = Guests::model()->Find('guest_id=:gid', array(':gid'=>$model->guest_id));
			$image = Pictures::model()->Find('picture_id=:pid', array(':pid'=>$guest->picture_id));
			
			$content .= "    <status>Duplicate</status>\n";
			$content .= "    <firstName>{$guest->first_name}</firstName>\n";
			$content .= "    <lastName>{$guest->last_name}</lastName>\n";
			$content .= "    <email>{$guest->email}</email>\n";
			if (is_null($image)) {
				$content .= "    <image></image>\n";
			} else {
				$content .= "    <image>" . Yii::app()->params['URL'] . Yii::app()->baseURL . '/guest_images/' . $image->path . "</image>\n";
			}
			
		} else if ($model->checked_in == 0) {
			//Good To Go
			
			$guest = Guests::model()->Find('guest_id=:gid', array(':gid'=>$model->guest_id));
			$image = Pictures::model()->Find('picture_id=:pid', array(':pid'=>$guest->picture_id));
			
			$content .= "    <status>Good</status>\n";
			$content .= "    <firstName>{$guest->first_name}</firstName>\n";
			$content .= "    <lastName>{$guest->last_name}</lastName>\n";
			$content .= "    <email>{$guest->email}</email>\n";
			if (is_null($image)) {
				$content .= "    <image></image>\n";
			} else {
				$content .= "    <image>" . Yii::app()->params['URL'] . Yii::app()->baseURL . '/guest_images/' . $image->path . "</image>\n";
			}
			//Set Checked In
			$model->checked_in = 1;
			$model->save();
		} else {
			//Other Error
		}
		$content .= "</response>\n";
		$this->render('index', array('content'=>$content));
	}
}